package net.root.plain.main;

import java.util.ArrayList;

import net.root.plain.alarm.AlarmListAdapter;
import net.root.plain.alarm.AlarmDAO;
import net.root.plain.alarm.AlarmDTO;
import net.root.plain.util.DBConn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class Tab2 extends Activity {
	private AlarmListAdapter adapter;
	private ArrayList<AlarmDTO> lists;
	private ListView listView;
	private Button btn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab2);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AlarmDAO dao = new AlarmDAO(DBConn.getConnection(this));
		lists = dao.getLists();
		DBConn.close();
		btn = (Button)findViewById(R.id.tab2_button);
		btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Tab2.this,AlarmEditActivity.class);
				startActivity(intent);
			}
		});
		listView = (ListView)findViewById(R.id.tab2_list_view);
		
		adapter = new AlarmListAdapter(this, R.layout.list_view_list_row, lists);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Tab2.this,AlarmEditActivity.class);
				intent.putExtra(DBConn.columns[0], lists.get(arg2).getNo());
				startActivity(intent);
			}
		});
		adapter.notifyDataSetChanged();
	}
}
